package day27;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.MessageFormat;
import java.util.Set;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;

public class MapSearchPanel extends JPanel {
	private JPanel panel;
	private JScrollPane scrollPane;
	private JTextArea textArea;
	private JTextField findStr;
	private JButton btnNewButton;
	private JLabel lblNewLabel;

	/**
	 * Create the panel.
	 */
	public MapSearchPanel() {
		setLayout(new BorderLayout(0, 0));
		add(getPanel(), BorderLayout.NORTH);
		add(getScrollPane(), BorderLayout.CENTER);
		add(getLblNewLabel(), BorderLayout.SOUTH);

	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getFindStr(), BorderLayout.CENTER);
			panel.add(getBtnNewButton(), BorderLayout.EAST);
		}
		return panel;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTextArea());
		}
		return scrollPane;
	}
	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
			textArea.setEditable(false);
			textArea.setFont(new Font("HY그래픽M", Font.PLAIN, 13));
		}
		return textArea;
	}
	private JTextField getFindStr() {
		if (findStr == null) {
			findStr = new JTextField();
			findStr.setColumns(10);
		}
		return findStr;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String format="%-10s\t%-10s\t%-10s\t%-20s";
					Data d = null;
					textArea.setText("");
					String str = String.format(format, "아이디", 
							"성명","생일","주소");
					textArea.append(str + "\n");
					
					if(findStr.getText().equals("")){//모든 데이터
						Set<String> key = MapFrame.map.keySet();
						for(String k : key){
							d = MapFrame.map.get(k);
							str = String.format(format, d.getId(), 
									d.getName(), d.getBirthday(), d.getAddress());
							textArea.append(str + "\n");
						}
					}else{
						Set<String> key = MapFrame.map.keySet();
						for(String k : key){
							d = MapFrame.map.get(k);
							if(d.compare(findStr.getText())){
								str = String.format(format, d.getId(), 
										d.getName(), d.getBirthday(), d.getAddress());
								textArea.append(str + "\n");
							}
						}
						
					}
				}
			});
		}
		return btnNewButton;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("검색할 자료를 입력하세요...");
			lblNewLabel.setBackground(Color.LIGHT_GRAY);
			lblNewLabel.setOpaque(true);
		}
		return lblNewLabel;
	}
}
